home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / dev / misc / evaluate.lha / eval.c next >
Encoding:
C/C++ Source or Header  |  2000-03-19  |  997 b   |  33 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "evaluate.h"
  5.  
  6. int main(int argc, char **argv) {
  7.   struct vartable *vt = create_vartable();
  8.   struct val p, e, result;
  9.   char buffer[512];
  10.  
  11.   e.type = T_REAL; e.rval = exp(1.0);
  12.   p.type = T_REAL; p.rval = 4.0 * atan(1.0);
  13.  
  14.   if (!vt || !put_var(vt, "e", &e) || !put_var(vt, "pi", &p))
  15.     return EXIT_FAILURE;
  16.  
  17.   printf("evaluate.c example demo. please enter expressions\n");
  18.   while (fgets(buffer, 512, stdin)) {
  19.     switch (evaluate(buffer, &result, vt)) {
  20.     case ERROR_SYNTAX:      printf("syntax error\n");       break;
  21.     case ERROR_VARNOTFOUND: printf("variable not found\n"); break;
  22.     case ERROR_NOMEM:       printf("not enough memory\n");  break;
  23.     case ERROR_DIV0:        printf("division by zero\n");   break;
  24.     case RESULT_OK: 
  25.       if (result.type == T_INT) printf("result = %ld\n", result.ival);
  26.       else printf("result = %g\n", result.rval);
  27.       
  28.     }
  29.   }
  30.   free_vartable(vt);
  31.   return EXIT_SUCCESS;
  32. }
  33.